package com.bangdream.michelia.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bangdream.michelia.utils.L;
import com.jaeger.library.StatusBarUtil;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.AdContract;
import com.bangdream.michelia.entity.AdBean;
import com.bangdream.michelia.presenter.AdPresenter;
import com.bangdream.michelia.utils.MyLog;
import com.bangdream.michelia.utils.NetworkUtil;
import com.bangdream.michelia.utils.SPUtils;
import com.bangdream.michelia.view.activity.base.BaseActivity;
import com.bangdream.michelia.view.activity.base.OnPermissionListener;
import com.bangdream.michelia.view.activity.currency.LoginActivity;
import com.bangdream.michelia.view.activity.currency.MainActivity;
import com.bangdream.michelia.view.activity.currency.WebViewActivity;

/**
 * Created by Mr.Wang on 2018/3/29.
 * 909732184@qq.com
 */
public class WelcomeActivity extends BaseActivity<AdContract.IAdView,AdPresenter<AdContract.IAdView>> implements AdContract.IAdView {
    private static final String TAG = "WelcomeActivity";
    private int timeCount;
    //广告最长显示5秒
    private int initTimeCount = -1;
    private boolean continueCount = true;
    private AdBean adBean;
    private ImageView ivAdvertising;
    private LinearLayout layoutSkip;
    private Bitmap adBitmap;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            countNum();
            if (continueCount) {
                handler.sendMessageDelayed(handler.obtainMessage(-1),1000);
            }
        }
    };

    @Override
    public int getLayout() {
        return R.layout.welcome;
    }

    @Override
    public void findId() {
        ivAdvertising = findViewById(R.id.ivAdvertising);
        layoutSkip = findViewById(R.id.layoutSkip);
    }

    @Override
    public void logic() {
        /**
         * 设置全屏并且显示状态栏
         */
        //5.0以上系统才支持这个效果 app内容占据导航栏 并且导航栏透明
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }else {
            StatusBarUtil.setColorNoTranslucent(this, ContextCompat.getColor(this, R.color.white));
        }

        PermissionApplication(PicturePerms, "为了正常使用本应用需要获取以下权限", new OnPermissionListener() {
            @Override
            public void onSuccess() {
                handler.sendMessageDelayed(handler.obtainMessage(-1),1000);
                if(NetworkUtil.isConnected(WelcomeActivity.this)) {
                    //mPresenter.startLoadData();
                }
                startActivity(new Intent(WelcomeActivity.this,LoginActivity.class));
                finish();
                //listSort2();
            }
            @Override
            public void onFail() {
                finish();
            }
        });
    }

    @Override
    public AdPresenter createPresenter() {
        AdPresenter adPresenter = new AdPresenter();
        //adPresenter.setPd(pd);
        return adPresenter;
    }

    @Override
    public void setListener() {
        layoutSkip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toNextActivity();
            }
        });
        ivAdvertising.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                continueCount = false;
                Intent intent = new Intent(WelcomeActivity.this,WebViewActivity.class);
                intent.putExtra("url",adBean.getStWebUrl());
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    public void setAdData(AdBean adData) {
        if(adData!=null){
            this.adBean = adData;
        }
    }

    @Override
    public void setAdImg(Bitmap bitmap) {
        this.adBitmap = bitmap;
        ivAdvertising.setImageBitmap(adBitmap);
    }

    @Override
    public void setAdTime(int nmTime) {
        this.initTimeCount = nmTime;
    }

    private int countNum() {
        //数秒
        timeCount++;
        MyLog.d(TAG,timeCount+"");
        if (timeCount == 3) {//数秒，超过3秒后如果没有网络，则进入下一个页面
            if (!NetworkUtil.isConnected(WelcomeActivity.this)) {
                MyLog.d(TAG,"没有网络");
                toNextActivity();
            }
            else if (adBean==null||!adBean.isPlayAd()||adBitmap==null) {//如果服务端不允许播放广告，则直接进入下一个页面
                MyLog.d(TAG,"没有获取到广告信息");
                toNextActivity();
            } else {
                MyLog.d(TAG,"显示广告图");
                ivAdvertising.setVisibility(View.VISIBLE);
                layoutSkip.setVisibility(View.VISIBLE);
            }
        }
        //如果计时数-3秒固定显示时间 大于等于广告显示时间则广告时间显示结束
        if(timeCount-3>=initTimeCount&&initTimeCount>-1) {
            toNextActivity();
        }
        return timeCount;
    }
    private void toNextActivity() {//根据是否保存有 token 判断去登录界面还是主界面
        continueCount = false;
        Intent intent = null;
        String token = (String) SPUtils.get(WelcomeActivity.this, "token", "");
        if (TextUtils.isEmpty(token)) {
            intent = new Intent(WelcomeActivity.this, LoginActivity.class);
        } else {
            intent = new Intent(WelcomeActivity.this, MainActivity.class);
        }
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adBitmap != null) {//进行bitmap回收
            adBitmap.recycle();
        }
        //清空消息队列
        if(handler!=null){
            handler.removeCallbacksAndMessages(null);
        }
    }
    //选择排序
    private void listSort1(){
        int [] datas = {2,3,51,3,61,36,235,12,6,23,235,1,32,754,2336,23,142,346,24,436,32,56357,2,6,432,6,32,54,134,54,32,12,344,34};
        for( int i = 0;i < datas.length-1;i++){
            for(int j = i+1;j<datas.length;j++){
                if(datas[i]>datas[j]){
                    datas[i] = datas[i] ^ datas[j];
                    datas[j] = datas[i] ^ datas[j];
                    datas[i] = datas[i] ^ datas[j];
//                    datas[i] = datas[i]*datas[j];
//                    datas[j] = datas[i]/datas[j];
//                    datas[i] = datas[i]/datas[j];
                }
            }
        }
        String str = "";
        for(int index:datas){
            str += index+"\t";
        }
        System.out.println(str+"");
    }
    //冒泡排序
    private void listSort2(){
        int [] datas = {2,3,51,3,61,36,235,12,6,23,235,1,32,754,2336,23,142,346,24,436,32,56357,2,6,432,6,32,54,134,54,32,12,344,34};
        for(int i = 0;i<datas.length-1;i++){
            boolean flag = true;
            for( int j = 0;j<datas.length-1-i;j++){
                if(datas[j]>datas[j+1]){
                    datas[j] = datas[j] ^ datas[j+1];
                    datas[j+1] = datas[j] ^ datas[j+1];
                    datas[j] = datas[j] ^ datas[j+1];
                    flag = false;
                }
            }
            if(flag){
                break;
            }
        }
        String str = "";
        for(int index:datas){
            str += index+"\t";
        }
        System.out.println(str+"");
    }

    //插入排序
    private void listSort3(){
        int [] datas = {2,3,51,3,61,36,235,12,6,23,235,1,32,754,2336,23,142,346,24,436,32,56357,2,6,432,6,32,54,134,54,32,12,344,34};
        for (int i = 1; i < datas.length; i++) {
            int j = i;
            while (j > 0 && datas[j] < datas[j - 1]) {
                datas[j] = datas[j] ^ datas[j-1];
                datas[j-1] = datas[j] ^ datas[j-1];
                datas[j] = datas[j] ^ datas[j-1];
                j--;
            }
        }
        String str = "";
        for(int index:datas){
            str += index+"\t";
        }
        System.out.println(str+"");
    }

}
